﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using Com.DAO;
using Com.Object;
using WF.Web.Pages;
using WF.Web.Pages.Workflow;
using WF.Common;

namespace WF.Web.Pages.Workflow.KQ
{
    public partial class KQApply : BasePageWorkflow
    {
        DataManager dataManager = new DataManager();

        #region 页面事件

        #region 页面加载
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    WF.Common.SessionManager sm = new SessionManager();
                    UserProfile user = sm.CurrentUser;

                    ((Button)FormView1.FindControl("btnCount")).Enabled = false;
                    ((DropDownList)FormView1.FindControl("ddlHourFrom")).Items.Clear();
                    ((DropDownList)FormView1.FindControl("ddlHourTo")).Items.Clear();
                    ((DropDownList)FormView1.FindControl("ddlMinuteFrom")).Items.Clear();
                    ((DropDownList)FormView1.FindControl("ddlMinuteTo")).Items.Clear();

                    for (int i = 0; i <= 59; i++)
                    {
                        if (i <= 23)
                        {
                            ((DropDownList)FormView1.FindControl("ddlHourFrom")).Items.Add(i.ToString("00"));
                            ((DropDownList)FormView1.FindControl("ddlHourTo")).Items.Add(i.ToString("00"));
                        }
                        ((DropDownList)FormView1.FindControl("ddlMinuteFrom")).Items.Add(i.ToString("00"));
                        ((DropDownList)FormView1.FindControl("ddlMinuteTo")).Items.Add(i.ToString("00"));
                    }
                    ((TextBox)FormView1.FindControl("txtRegisterDate")).Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ((TextBox)FormView1.FindControl("txtDateFrom")).Text = System.DateTime.Now.ToString("yyyy-MM-dd");



                    ((HiddenField)FormView1.FindControl("txtFlowID")).Value = System.Guid.NewGuid().ToString();

                    ((HyperLink)FormView1.FindControl("lnkUpload")).NavigateUrl = "../../../Attachment/UploadAttachment.aspx?KQAPPLYID=" + ((HiddenField)FormView1.FindControl("txtFlowID")).Value;
                    ((HyperLink)FormView1.FindControl("lnkUpload")).Attributes.Add("onclick", "return ShowDialog(this, 350, 150, '');");
                    ((HyperLink)FormView1.FindControl("lnkView")).NavigateUrl = "Attachment.aspx?KQAPPLYID=" + ((HiddenField)FormView1.FindControl("txtFlowID")).Value;
                    ((HyperLink)FormView1.FindControl("lnkView")).Attributes.Add("onclick", "return ShowDialog(this, 650, 450, '');");

                    ((DropDownList)FormView1.FindControl("ddlHourFrom")).SelectedValue = "09";
                    ((DropDownList)FormView1.FindControl("ddlMinuteFrom")).SelectedValue = "00";
                    ((DropDownList)FormView1.FindControl("ddlHourTo")).SelectedValue = "18";
                    ((DropDownList)FormView1.FindControl("ddlMinuteTo")).SelectedValue = "00";

                    ((TextBox)FormView1.FindControl("txtDateTo")).Text = ((TextBox)FormView1.FindControl("txtDateFrom")).Text;

                    this.LoadHoliday();
                    this.LoadUrlPara();

                    //处理预设定参数
                    if (Request.QueryString["basicTime"] != null)
                    {
                        DateTime basictime = Convert.ToDateTime(Request.QueryString["basicTime"]);
                        ((TextBox)FormView1.FindControl("txtDateFrom")).Text = basictime.ToString("yyyy-MM-dd");
                        ((DropDownList)FormView1.FindControl("ddlHourFrom")).SelectedValue = basictime.Hour.ToString();
                        ((DropDownList)FormView1.FindControl("ddlHourTo")).SelectedValue = basictime.Minute.ToString();
                    }
                    if (Request.QueryString["ApplyTypeCode"] != null)
                    {
                        ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue = Request.QueryString["ApplyTypeCode"].ToString();
                    }

                    this.LoadRegisterInfo();
                    string identityName = User.Identity.Name;
                    if (identityName == null)
                    {
                        identityName = "train8";
                    }
                    this.LoadEmptInfo(identityName);

                    LoadInitCardTims(((Label)FormView1.FindControl("lblInitCardTimes")), ((TextBox)FormView1.FindControl("txtDomainAccount")).Text, ((TextBox)FormView1.FindControl("txtDateFrom")).Text, ((TextBox)FormView1.FindControl("txtDateTo")).Text);

                }
                ((Label)this.FormView1.FindControl("lblErrorDescription")).Text = "";
                ((Label)this.FormView1.FindControl("lblErrorDescription")).Text = "";
            }
            catch (Exception ex)
            {
                //((Button)FormView1.FindControl("btnSelect")).Enabled = false;
                //((Button)FormView1.FindControl("btnCount")).Enabled = false;
                ((Button)FormView1.FindControl("btnSubmit")).Enabled = false;
                //((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).Enabled = false;

                if (ex.Message.IndexOf("System.Exception") > 0)
                {
                    int errorWord = ex.Message.IndexOf("System.Exception:") + ("System.Exception:").Length;
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message.Substring(errorWord, ex.Message.IndexOf("在 WSK") - errorWord);
                }
                else
                {
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                }

                //this.k2btnSubmit.Enabled = false;
                //((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                ((Label)FormView1.FindControl("lblErrorDescription")).ForeColor = System.Drawing.Color.Red;
                //if (Page.User.Identity.Name.ToLower() == "centaline\\wufw")
                //{
                //    this.Panel1.Visible = true;
                //    this.btnSelect.Enabled = true;
                //}
                //else
                //{
                //    this.Panel1.Visible = false;
                //}
            }
        }
        #endregion

        #region 计算按钮事件
        public void btnCount_Click(object sender, EventArgs e)
        {
            try
            {
                this.ComputerHolidayHours();
                ((HiddenField)FormView1.FindControl("hiddFlag")).Value = "1";
                ((Button)FormView1.FindControl("btnSubmit")).Enabled = true;
            }
            catch (Exception ex)
            {
                ((HiddenField)FormView1.FindControl("hiddFlag")).Value = "0";
                if (ex.Message.IndexOf("System.Exception") > 0)
                {
                    int errorWord = ex.Message.IndexOf("System.Exception:") + ("System.Exception:").Length;
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message.Substring(errorWord, ex.Message.IndexOf("at WSKQ") - errorWord);
                }
                else
                {
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                }

                //((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                ((Label)FormView1.FindControl("lblErrorDescription")).ForeColor = System.Drawing.Color.Red;
                ((Button)FormView1.FindControl("btnSubmit")).Enabled = false;
                //this.k2btnSubmit.Enabled = false;
                //this.btnSubmit.Enabled = false;
            }
        }
        #endregion

        #region 假期类型选择
        protected void ddlApplyTypeCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            decimal LeftHours = 0;

            try
            {
                SHZYKQ.KQ mobjKQ = new SHZYKQ.KQ();
                ((HiddenField)FormView1.FindControl("txtDatetimeFrom")).Value = ((TextBox)FormView1.FindControl("txtDateFrom")).Text + " " + ((DropDownList)FormView1.FindControl("ddlHourFrom")).SelectedValue + ":" + ((DropDownList)FormView1.FindControl("ddlMinuteFrom")).SelectedValue + ":00";
                ((HiddenField)FormView1.FindControl("txtDatetimeTo")).Value = ((TextBox)FormView1.FindControl("txtDateTo")).Text + " " + ((DropDownList)FormView1.FindControl("ddlHourTo")).SelectedValue + ":" + ((DropDownList)FormView1.FindControl("ddlMinuteTo")).SelectedValue + ":00";
                mobjKQ.CheckRepeatBill(((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value, ((HiddenField)FormView1.FindControl("txtFlowID")).Value, ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue, ((HiddenField)FormView1.FindControl("txtDatetimeFrom")).Value, ((HiddenField)FormView1.FindControl("txtDatetimeTo")).Value);

                DropDownList ddlApplyTypeCode = (DropDownList)FormView1.Row.FindControl("ddlApplyTypeCode");
                if (ddlApplyTypeCode.SelectedValue == null)
                {
                    return;
                }
                ((HiddenField)FormView1.FindControl("txtHolidayIdentify")).Value = ddlApplyTypeCode.SelectedValue;

                Com.Object.Fields fd = new Fields();

                SHZYKQ.KQ pobj = new SHZYKQ.KQ();
                //pobj.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Data.DataSet ds = pobj.GetHolidayList("HolidayIdentify='" + ddlApplyTypeCode.SelectedValue + "'");
                fd.LoadDataFromDataSet(ds, true);

                if (string.IsNullOrEmpty(fd.GetFieldValue("FlowType")))
                    throw new BusinessException("没有找到该假期类型所对应的流程类型");

                if (string.IsNullOrEmpty(ddlApplyTypeCode.SelectedItem.Text))
                    throw new BusinessException("假期类型选择不成功，请重新选择");


                ((HiddenField)FormView1.FindControl("txtWorkflowType")).Value = fd.GetFieldValue("FlowType");
                ((HiddenField)FormView1.FindControl("txtWorkflowTypeSub")).Value = fd.GetFieldValue("FlowTypeSub");
                ((HiddenField)FormView1.FindControl("txtHolidayTypeName")).Value = ddlApplyTypeCode.SelectedItem.Text;
                ((HiddenField)FormView1.FindControl("txtFullQQBillDays")).Value = fd.GetFieldValue("FullQQBillDays");

                LeftHours = pobj.GetHolidayLeftHours(((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value, ddlApplyTypeCode.SelectedValue);

                if (fd.GetFieldValue("HolidayName") != "")
                {
                    ((Label)FormView1.FindControl("lblHospitalDisplay")).Text = fd.GetFieldValue("HolidayName") + " 的最小申请时间为 <font color=red>" + fd.GetFieldValue("MinuteMin") + "</font> 分钟，申请基数为 <font color=red>" + fd.GetFieldValue("MinuteBase") + "</font> 分钟";
                    if (LeftHours != -9999)
                    {
                        ((Label)FormView1.FindControl("lblHospitalDisplay")).Text = ((Label)FormView1.FindControl("lblHospitalDisplay")).Text + "，您目前的可用时数为 <font color=red>" + (LeftHours / 60).ToString() + "</font> 小时/<font color=red>" + LeftHours.ToString() + "</font> 分钟";

                    }
                    else
                    {
                        ((Label)FormView1.FindControl("lblHospitalDisplay")).Text = "";
                    }
                    ((Button)this.FormView1.FindControl("btnSelect")).Enabled = true;

                    //if (string.IsNullOrEmpty(((TextBox)FormView1.FindControl("txtReason")).Text))
                    //{
                    //    ((TextBox)FormView1.FindControl("txtReason")).Focus();
                    //    ((Button)this.FormView1.FindControl("btnCount")).Enabled = false;
                    //    throw new Exception("请先填写请假原因");
                    //}
                    ((Button)this.FormView1.FindControl("btnCount")).Enabled = true;
                    //((Button)this.FormView1.FindControl("btnSubmit")).Enabled = false;

                    if (((HiddenField)FormView1.FindControl("txtWorkflowType")).Value == "2")
                    {
                        ((Panel)FormView1.FindControl("pnlWJDK")).Visible = true;
                    }
                    else
                    {
                        ((Panel)FormView1.FindControl("pnlWJDK")).Visible = false;
                    }

                    //if (this.txtWorkflowTypeSub.Text.Trim() == "1")
                    //{
                    //    this.pnlAttachmentDoc.Visible = true;
                    //}
                    //else
                    //{
                    //    this.pnlAttachmentDoc.Visible = false;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.IndexOf("System.Exception") > 0)
                {
                    int errorWord = ex.Message.IndexOf("System.Exception:") + ("System.Exception:").Length;
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message.Substring(errorWord, ex.Message.IndexOf("at WSKQ") - errorWord);
                }
                else
                {
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                }
                //((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                ((Button)this.FormView1.FindControl("btnCount")).Enabled = true;
                ((Button)this.FormView1.FindControl("btnSubmit")).Enabled = false;
            }
        }
        #endregion

        #region 员工号查询
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                this.LoadEmptInfo(((TextBox)FormView1.FindControl("txtDomainAccount")).Text.Trim());

                ((HiddenField)this.FormView1.FindControl("txtApplyorAccount")).Value = ((TextBox)FormView1.FindControl("txtDomainAccount")).Text.Trim();
                LoadInitCardTims(((Label)FormView1.FindControl("lblInitCardTimes")), ((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value, ((TextBox)FormView1.FindControl("txtDateFrom")).Text, ((TextBox)FormView1.FindControl("txtDateTo")).Text);

                if (string.IsNullOrEmpty(((TextBox)FormView1.FindControl("txtReason")).Text))
                {
                    ((TextBox)FormView1.FindControl("txtReason")).Focus();
                    ((Button)this.FormView1.FindControl("btnCount")).Enabled = false;
                    return;
                }
                DropDownList ddlApplyTypeCode = ((DropDownList)FormView1.FindControl("ddlApplyTypeCode"));
                if (string.IsNullOrEmpty(ddlApplyTypeCode.SelectedValue))
                {
                    ((Button)FormView1.FindControl("btnCount")).Enabled = false;
                    return;
                }
                ((Button)FormView1.FindControl("btnCount")).Enabled = true;
                //((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).Enabled = true;
                //this.ddlApplyTypeCode_SelectedIndexChanged(null, null);
            }
            catch (Exception ex)
            {

                ((Button)FormView1.FindControl("btnCount")).Enabled = false;
                //((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).Enabled = false;
                //this.k2btnSubmit.Enabled = false;
                if (ex.Message.IndexOf("System.Exception") > 0)
                {
                    int errorWord = ex.Message.IndexOf("System.Exception:") + ("System.Exception:").Length;
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message.Substring(errorWord, ex.Message.IndexOf("在 WS") - errorWord);
                }
                else
                {
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                }

                //((Label)FormView1.FindControl("lblErrorDescription")).Text = ex.Message;
                ((Button)FormView1.FindControl("btnSubmit")).Enabled = false;
            }
        }
        #endregion

        #endregion

        #region 私有方法

        //打卡记录
        public static void LoadInitCardTims(System.Web.UI.WebControls.Label vLabel, string vAccount, string vDateForm, string vDateTo)
        {
            SHZYKQ.KQ mobjKQ = new SHZYKQ.KQ();
            try
            {
                vLabel.Text = "当前申请日期内的刷卡信息如下：" + mobjKQ.GetInitCardTimes(vAccount, vDateForm, vDateTo);
            }
            catch (Exception ex)
            {
                vLabel.Text = "获取刷卡信息错误：" + ex.Message;
            }
        }

        //假期类型加载
        private void LoadHoliday()
        {
            try
            {
                SHZYKQ.KQ pobj = new SHZYKQ.KQ();
                //pobj.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Data.DataSet ds = pobj.GetHolidayList("");

                DropDownList ddlApplyType = ((DropDownList)FormView1.FindControl("ddlApplyTypeCode"));
                ddlApplyType.DataSource = ds.Tables[0].DefaultView;
                ddlApplyType.DataTextField = "HolidayName";
                ddlApplyType.DataValueField = "HolidayIdentify";
                ddlApplyType.DataBind();

                System.Web.UI.WebControls.ListItem blankItem = new ListItem("--请选择--", "");
                ddlApplyType.Items.Insert(0, blankItem);

                //chonggs added 2013.5.26 针对使用对象为中介部，故取消“停薪留职”、“补签卡”、“加班单”“加班补休”类别选项
                object[] args = new string[] { "停薪留职", "补签卡", "加班单", "加班补休" };

                for (int i = 0; i < args.Length; i++)
                {
                    ListItem item = ddlApplyType.Items.FindByText(args[i].ToString());
                    if (item != null)
                    {
                        ddlApplyType.Items.Remove(item);
                    }
                }

            }
            catch (Exception ex)
            {
                ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).Enabled = false;
                throw new Exception("读取考勤设置信息=>假期类型加载出错：" + ex.Message);
            }
        }

        //加载注册信息
        private void LoadRegisterInfo()
        {
            string RegisterAccount = "";
            try
            {
                RegisterAccount = User.Identity.Name;
                RegisterAccount = RegisterAccount.ToLower();
                RegisterAccount = RegisterAccount.Replace("centaline\\", "");

                ((HiddenField)FormView1.FindControl("txtRegisterAccount")).Value = RegisterAccount;

                SHZYK3.K3 pobj = new SHZYK3.K3();
                //pobj.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Data.DataSet ds = pobj.GetEmpInfoByDomainAccount(User.Identity.Name);

                Com.Object.Fields fd = new Fields();
                fd.LoadDataFromDataSet(ds, true);
                ((HiddenField)FormView1.FindControl("txtRegisterName")).Value = fd.GetFieldValue("EmpName");
            }
            catch (Exception)
            {
                if (RegisterAccount == "train8")
                {
                    ((HiddenField)FormView1.FindControl("txtRegisterName")).Value = "崇高尚";
                }
            }
            finally
            {
                ((Label)FormView1.FindControl("lblErrorDescription")).Text = ((HiddenField)FormView1.FindControl("txtRegisterName")).Value + "[" + ((HiddenField)FormView1.FindControl("txtRegisterAccount")).Value + "]";
            }

        }

        //加载员工信息
        private void LoadEmptInfo(string vAccount)
        {
            try
            {
                //检查帐号的有效性
                AD.AD pobjUtility = new AD.AD();
                //pobjUtility.Credentials = System.Net.CredentialCache.DefaultCredentials;

                SHZYK3.K3 pobj = new SHZYK3.K3();
                //pobj.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Data.DataSet ds = pobj.GetEmpInfoByDomainAccount(vAccount);

                Com.Object.Fields fd = new Fields();
                fd.LoadDataFromDataSet(ds, true);

                ((TextBox)FormView1.FindControl("txtApplyor")).Text = fd.GetFieldValue("EmpName");
                ((TextBox)FormView1.FindControl("txtApplyor1")).Text = fd.GetFieldValue("EmpName");
                ((TextBox)FormView1.FindControl("txtDeptName")).Text = fd.GetFieldValue("DeptName");
                ((HiddenField)FormView1.FindControl("txtEmptLevel")).Value = fd.GetFieldValue("EmpLevel");
                ((HiddenField)FormView1.FindControl("txtAuditorPositionID")).Value = fd.GetFieldValue("PositionID");
                ((HiddenField)FormView1.FindControl("txtEmail")).Value = fd.GetFieldValue("Email");
                ((HiddenField)FormView1.FindControl("txtDeptCode")).Value = fd.GetFieldValue("DeptCode");

                string domainAccount = fd.GetFieldValue("DomainAccount");
                domainAccount = domainAccount.Split('@')[0];// +"@centaline.com.cn";
                ((TextBox)FormView1.FindControl("txtApplyor")).Text = domainAccount;
                ((TextBox)FormView1.FindControl("txtDomainAccount")).Text = domainAccount;
                ((HiddenField)this.FormView1.FindControl("txtApplyorAccount")).Value = domainAccount;

                //if (pobjUtility.CheckADUserExist("", domainAccount) == false)
                //{
                //    throw new Exception("[" + fd.GetFieldValue("EmpName") + "]的公司账号[" + vAccount + "]无效");
                //}
            }
            catch (Exception ex)
            {
                ((TextBox)FormView1.FindControl("txtApplyor")).Text = "";
                ((TextBox)FormView1.FindControl("txtApplyor")).Text = "";
                ((TextBox)FormView1.FindControl("txtDeptName")).Text = "";
                ((HiddenField)FormView1.FindControl("txtEmptLevel")).Value = "";
                ((HiddenField)FormView1.FindControl("txtFolio")).Value = "";
                //((HiddenField)FormView1.FindControl("txtAuditorPositionID")).Value = "";
                ((TextBox)FormView1.FindControl("txtApplyor")).Text = "";

                throw new Exception("获取申请者[" + vAccount + "]信息出错：" + ex.Message);
            }
        }

        private void LoadUrlPara()
        {
            System.DateTime basicTime;
            string ApplyTypeCode = "";
            string DataFrom = "";
            string HourFrom = "";
            string MinuteFrom = "";

            try
            {
                if (Request.QueryString.Get("basicTime") != null)
                {
                    try
                    {
                        basicTime = System.DateTime.Parse(Request.QueryString.Get("basicTime"));
                        DataFrom = basicTime.Date.ToString("yyyy-MM-dd");
                        HourFrom = basicTime.Hour.ToString("00");
                        MinuteFrom = basicTime.Minute.ToString("00");

                        ((TextBox)FormView1.FindControl("txtDateFrom")).Text = DataFrom;
                        ((TextBox)FormView1.FindControl("txtDateTo")).Text = DataFrom;
                        if (((DropDownList)FormView1.FindControl("ddlHourFrom")).Items.FindByValue(HourFrom) != null)
                        {
                            ((DropDownList)FormView1.FindControl("ddlHourFrom")).SelectedValue = HourFrom;
                        }
                        if (((DropDownList)FormView1.FindControl("ddlMinuteFrom")).Items.FindByValue(HourFrom) != null)
                        {
                            ((DropDownList)FormView1.FindControl("ddlMinuteFrom")).SelectedValue = MinuteFrom;
                        }

                        if (((DropDownList)FormView1.FindControl("ddlHourTo")).Items.FindByValue(HourFrom) != null)
                        {
                            ((DropDownList)FormView1.FindControl("ddlHourTo")).SelectedValue = HourFrom;
                        }
                        if (((DropDownList)FormView1.FindControl("ddlMinuteTo")).Items.FindByValue(HourFrom) != null)
                        {
                            ((DropDownList)FormView1.FindControl("ddlMinuteTo")).SelectedValue = MinuteFrom;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                if (Request.QueryString.Get("ApplyTypeCode") != null)
                {
                    ApplyTypeCode = Request.QueryString.Get("ApplyTypeCode");
                    if (((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).Items.FindByValue(ApplyTypeCode) != null)
                    {
                        ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue = MinuteFrom;
                    }
                }

            }
            catch (Exception)
            {
            }
        }

        private void ComputerHolidayHours()
        {
            try
            {
                System.DateTime DatetimeFrom;
                System.DateTime DatetimeTo;

                System.DateTime ExceptDatetimeFrom;
                System.DateTime ExceptDatetimeTo;

                DateTime CurrentMonth;
                CurrentMonth = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-01"));

                SHZYKQ.KQ mobjKQ = new SHZYKQ.KQ();

                if (((TextBox)FormView1.FindControl("txtApplyor")).Text.Trim().Length == 0)
                {
                    throw new Exception("您没有填写申请者！");
                }
                if (string.IsNullOrEmpty(((TextBox)FormView1.FindControl("txtReason")).Text))
                {
                    ((TextBox)FormView1.FindControl("txtReason")).Focus();
                    throw new Exception("请假原因不能为空");
                }
                if (string.IsNullOrEmpty(((TextBox)FormView1.FindControl("txtDateFrom")).Text))
                    throw new Exception("请假开始日期不能为空");
                if (string.IsNullOrEmpty(((TextBox)FormView1.FindControl("txtDateTo")).Text))
                    throw new Exception("请假结束日期不能为空");

                //chonggs added 2013.5.26 不得往前跨月申请各类假期（以申请日期为节点，如5/22申请可填写任意5月各类假期，但不能申请4月）
                if (System.Configuration.ConfigurationManager.AppSettings["IsCrossMonthCanSubmit"] == "0")
                {
                    if (CurrentMonth > DateTime.Parse(((TextBox)FormView1.FindControl("txtDateFrom")).Text))
                    {
                        throw new Exception("不允许进行之前月份单据提交，请重新选择日期！");
                    }
                }

                #region
                DropDownList ddlApplyTypeCode = (DropDownList)FormView1.Row.FindControl("ddlApplyTypeCode");
                ((HiddenField)FormView1.FindControl("txtHolidayIdentify")).Value = ddlApplyTypeCode.SelectedValue;

                Com.Object.Fields fd = new Fields();
                SHZYKQ.KQ pobj = new SHZYKQ.KQ();
                System.Data.DataSet ds = pobj.GetHolidayList("HolidayIdentify='" + ddlApplyTypeCode.SelectedValue + "'");
                fd.LoadDataFromDataSet(ds, true);
                ((HiddenField)FormView1.FindControl("txtWorkflowType")).Value = fd.GetFieldValue("FlowType");
                ((HiddenField)FormView1.FindControl("txtWorkflowTypeSub")).Value = fd.GetFieldValue("FlowTypeSub");
                ((HiddenField)FormView1.FindControl("txtHolidayTypeName")).Value = ddlApplyTypeCode.SelectedItem.Text;
                ((HiddenField)FormView1.FindControl("txtFullQQBillDays")).Value = fd.GetFieldValue("FullQQBillDays");
                #endregion

                ((HiddenField)FormView1.FindControl("txtDatetimeFrom")).Value = ((TextBox)FormView1.FindControl("txtDateFrom")).Text + " " + ((DropDownList)FormView1.FindControl("ddlHourFrom")).SelectedValue + ":" + ((DropDownList)FormView1.FindControl("ddlMinuteFrom")).SelectedValue + ":00";
                ((HiddenField)FormView1.FindControl("txtDatetimeTo")).Value = ((TextBox)FormView1.FindControl("txtDateTo")).Text + " " + ((DropDownList)FormView1.FindControl("ddlHourTo")).SelectedValue + ":" + ((DropDownList)FormView1.FindControl("ddlMinuteTo")).SelectedValue + ":00";
                LoadInitCardTims(((Label)FormView1.FindControl("lblInitCardTimes")), ((HiddenField)FormView1.FindControl("txtRegisterAccount")).Value, ((TextBox)FormView1.FindControl("txtDateFrom")).Text, ((TextBox)FormView1.FindControl("txtDateTo")).Text);

                mobjKQ.CheckRepeatBill(((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value, ((HiddenField)FormView1.FindControl("txtFlowID")).Value, ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue, ((HiddenField)FormView1.FindControl("txtDatetimeFrom")).Value, ((HiddenField)FormView1.FindControl("txtDatetimeTo")).Value);

                if (((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue.ToString().ToUpper() == "JB" || ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue.ToString().ToUpper() == "JBD")
                {
                    DatetimeFrom = System.DateTime.Parse(((HiddenField)FormView1.FindControl("txtDatetimeFrom")).Value);
                    DatetimeTo = System.DateTime.Parse(((HiddenField)FormView1.FindControl("txtDatetimeTo")).Value);

                    ExceptDatetimeFrom = System.DateTime.Parse(DatetimeFrom.ToString("yyyy-MM-dd") + " 12:00");
                    ExceptDatetimeTo = System.DateTime.Parse(DatetimeTo.ToString("yyyy-MM-dd") + " 13:00");

                    System.TimeSpan pobjTimeSpan = DatetimeTo - DatetimeFrom;

                    if (DatetimeFrom <= ExceptDatetimeFrom && DatetimeTo >= ExceptDatetimeTo)
                    {
                        ((TextBox)FormView1.FindControl("txtApplyDays")).Text = pobjTimeSpan.TotalDays.ToString("0.0");
                        ((TextBox)FormView1.FindControl("txtApplyHours")).Text = (pobjTimeSpan.TotalHours - 1).ToString("0.0");
                    }
                    else
                    {
                        ((TextBox)FormView1.FindControl("txtApplyDays")).Text = pobjTimeSpan.TotalDays.ToString("0.0");
                        ((TextBox)FormView1.FindControl("txtApplyHours")).Text = pobjTimeSpan.TotalHours.ToString("0.0");
                    }

                    mobjKQ.CheckHolidayHours(((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue, ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedItem.Text, (decimal)pobjTimeSpan.TotalMinutes, ((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value);
                    return;
                }

                double[] rtn = null;
                //SHLZKQ.KQ pobj = new SHLZKQ.KQ();
                //pobj.Credentials = System.Net.CredentialCache.DefaultCredentials;
                rtn = pobj.GetEmpTotalWorkTime(((TextBox)FormView1.FindControl("txtDomainAccount")).Text.Trim(), System.DateTime.Parse(((HiddenField)FormView1.FindControl("txtDatetimeFrom")).Value), System.DateTime.Parse(((HiddenField)FormView1.FindControl("txtDatetimeTo")).Value));

                if (rtn == null)
                {
                    throw new Exception("未能正确计算出单据的假期时间。");
                }

                ((TextBox)FormView1.FindControl("txtApplyDays")).Text = Math.Round(rtn[1], 1).ToString();
                ((TextBox)FormView1.FindControl("txtApplyHours")).Text = Math.Round(rtn[0], 1).ToString();


                mobjKQ.CheckHolidayHours(((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue, ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedItem.Text, (decimal)rtn[2], ((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value);

                //
                CheckHolidayHoursSH(((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedValue, ((DropDownList)FormView1.FindControl("ddlApplyTypeCode")).SelectedItem.Text, (decimal)rtn[2], ((HiddenField)FormView1.FindControl("txtApplyorAccount")).Value);

                this.dataManager.OpenWithConfig();
                if (((HiddenField)FormView1.FindControl("txtWorkflowTypeSub")).Value == "1" && WF.BusinessObject.Attachment.GetAttachementNum(((HiddenField)FormView1.FindControl("txtFlowID")).Value, this.dataManager) <= 0)
                {
                    throw new Exception("您申请的假期需要证明材料，请上传附件！");
                }
                this.dataManager.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void CheckProvePersonAccount(string vAccount)
        {
            //检查帐号的有效性
            AD.AD pobjUtility = new AD.AD();

            SHZYK3.K3 pobj = new SHZYK3.K3();

            System.Data.DataSet ds = pobj.GetEmpInfoByDomainAccount(vAccount);

            Com.Object.Fields fd = new Fields();
            fd.LoadDataFromDataSet(ds, true);

            string domainAccount = fd.GetFieldValue("DomainAccount");
            domainAccount = domainAccount.Split('@')[0];// +"@centaline.com.cn";

            //if (pobjUtility.CheckADUserExist("", domainAccount) == false)
            //{
            //    throw new Exception("[" + fd.GetFieldValue("EmpName") + "]的公司账号[" + vAccount + "]无效");
            //}
        }

        private void CheckHolidayHoursSH(string vHolidayIdentity, string vHolidayName, decimal vHolidayHours, string vDomainAccount)
        {

        }

        #endregion

        protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            try
            {
                this.Master.HandleException(e, false);
                if (e.Exception == null)
                {
                    this.ShowMessage("操作成功", true, true);
                }
                else
                {
                    if (e.Exception.Message.IndexOf("System.Exception") > 0)
                    {
                        int errorWord = e.Exception.Message.IndexOf("System.Exception:") + ("System.Exception:").Length;
                        ((Label)FormView1.FindControl("lblErrorDescription")).Text = e.Exception.Message.Substring(errorWord, e.Exception.Message.IndexOf("at WSKQ") - errorWord);
                    }
                    else
                    {
                        ((Label)FormView1.FindControl("lblErrorDescription")).Text = e.Exception.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                if (e.Exception.Message.IndexOf("System.Exception") > 0)
                {
                    int errorWord = e.Exception.Message.IndexOf("System.Exception:") + ("System.Exception:").Length;
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = e.Exception.Message.Substring(errorWord, e.Exception.Message.IndexOf("at WSKQ") - errorWord);
                }
                else
                {
                    ((Label)FormView1.FindControl("lblErrorDescription")).Text = e.Exception.Message;
                }
            }
        }
    }
}